<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['params']=array();
$merchid=$this->checklogin();
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';

$return['ispay']=array('0'=>'未支付','1'=>'已支付');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'api_pluginname'=>'jy_supplier');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;	
    $orderby='createtime desc';
	$total='';
	if(trim($_RQ['ordersn'])){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
		$ids=pdo_getall('jy_weishop_order',$params,'id','id');
		if($ids){
			$ids=array_keys($ids);
		}else{
			$ids=-1;
		}
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'api_id'=>$merchid,'api_pluginname'=>'jy_supplier','clearstatus'=>0);
	if($ids){
		$params['orderid']=$ids;
	}
	$return['list']=pdo_getslice('jy_weishop_order_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$gv){
			$order=pdo_get('jy_weishop_order',array('id'=>$gv['orderid']),array('ordersn','createtime'));
			$gv['ordersn']=$order['ordersn'];
			$gv['createtime']=$order['createtime'];
			$gv['ordergoodsid']=$gv['id'];
			$gv['id']=$gv['orderid'];
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','costprice','image','title','api_id'));
			$gv['image']=$goodsinfo['image'];
			$gv['title']=$goodsinfo['title'];
			$gv['goodsinfo']=$gv['goodsinfo']?json_decode($gv['goodsinfo'],true):array();
			$goodsinfo['costprice']=$gv['goodsinfo']['costprice']?:$goodsinfo['costprice'];
			$gv['total_costprice']=$gv['total']*$goodsinfo['costprice'];
			//商品规格
			if($gv['optionid']){
				$option['costprice']=$gv['goodsinfo']['costprice'];
				if(!$option['costprice']){
					$option=pdo_get('jy_weishop_goods_option',array('id'=>$optionid),array('costprice','specs'));
				}
				$gv['selespename']=$gv['optiondata'];
				$gv['total_costprice']=$gv['total']*$option['costprice'];
			}
			$gv['selespename']=$gv['selespename']?:'无';
			$gv['fee_desc']=$gv['fee_desc']?json_decode($gv['fee_desc'],true):array();
			//if(!$gv['fee_desc']){
				$gv['fee_desc']=model('jy_weishop/order')->setfee_desc($gv['orderid'],true);
				$gv['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['ordergoodsid']),'total_price');
			//}
			$gv['dispatchprice']=$gv['fee_desc']['dispatchprice'];
			//计算佣金
			if($this->plugin_exists('jy_commission')){
				$gv['commission_money']=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['ordergoodsid']);
			}
			$gv['merchorder']=$this->model('commission')->get_choufee($gv['orderid'],true,$gv['merchid']);
			$gv['merchorder_commission_money']=$gv['merchorder']['commission_money']?:0;
			if($gv['merchorder']['status']){
				$gv['merchorder_statustitle']='可结算';
			}else{
				$gv['merchorder_statustitle']=$gv['merchorder']['commission_money']?'不可结算':'未成交';
			}
	}
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'待结算订单/商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：如果商品规格已删除','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('type'=>'img_string','title'=>'商品','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'规格：','name'=>'selespename')
				)
			),
			array('type'=>'img_string','title'=>'订单分额',
			'data'=>array(
				array('title'=>'金额：￥','name'=>'total_price'),
				array('title'=>'运费：￥','name'=>'dispatchprice')
				)
			),
			array('name'=>'total_costprice','title'=>'供应成本','type'=>'string'),
			array('name'=>'commission_money','title'=>'需扣除佣金','type'=>'string'),
			array('type'=>'img_string','title'=>'抽成情况',
			'data'=>array(
				array('title'=>'金额：￥','name'=>'merchorder_commission_money'),
				array('title'=>'状态：','name'=>'merchorder_statustitle')
				)
			),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),		
		),
		'action'=>array(	
			array('type'=>'href','action'=>'manage.order_detail/','title'=>'订单详情'),		
		),),
		
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/order_index');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$params['api_id']=$merchid;
	$params['api_pluginname']='jy_supplier';
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('1'=>'微信支付','2'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),'goodsid');
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】'.$goodstitle;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'金额',
		'paytype'=>'订单状态',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}

?>